package cn.gp.chat.mapper;

import cn.gp.chat.entity.User;
import cn.gp.chat.commom.resp.UserVo;
import cn.gp.chat.commom.resp.FriendRequestVo;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface UserMapper {
    @Select("SELECT id, username, password, nickname FROM users WHERE username = #{username}")
    User selectByUsername(String username);

    @Select("SELECT COUNT(*) FROM users WHERE username = #{username}")
    boolean existsByUsername(String username);

    @Insert("INSERT INTO users(username, password, nickname) " +
            "VALUES(#{username}, #{password}, #{nickname})")
    void insert(User user);

    @Select("SELECT * FROM users WHERE username = #{username}")
    User findByUsername(String username);

    @Select("SELECT * FROM users WHERE id = #{id}")
    User findById(Long id);

    @Update("UPDATE users SET nickname = #{nickname}, avatar = #{avatar} WHERE id = #{id}")
    void updateProfile(User user);

    // 查询用户好友列表
    @Select("SELECT u.id, u.username, u.nickname, u.avatar, u.status, f.remark " +
            "FROM friendships f " +
            "JOIN users u ON f.friend_id = u.id " +
            "WHERE f.user_id = #{userId} AND f.status = 1")
    List<UserVo.FriendVo> selectFriendsByUserId(Long userId);

    @Select("SELECT * FROM users WHERE username LIKE CONCAT('%', #{keyword}, '%') " +
            "OR nickname LIKE CONCAT('%', #{keyword}, '%')")
    List<User> searchUsers(String keyword);

    @Select("SELECT COUNT(*) FROM friendships " +
            "WHERE (user_id = #{userId1} AND friend_id = #{userId2}) " +
            "OR (user_id = #{userId2} AND friend_id = #{userId1})")
    boolean existsFriendship(Long userId1, Long userId2);

    @Select("SELECT COUNT(*) FROM friend_requests " +
            "WHERE requester_id = #{requesterId} AND receiver_id = #{receiverId} " +
            "AND status = 0")
    boolean existsFriendRequest(Long requesterId, Long receiverId);

    @Insert("INSERT INTO friend_requests(requester_id, receiver_id, request_message, status) " +
            "VALUES(#{requesterId}, #{receiverId}, #{message}, 0)")
    void insertFriendRequest(Long requesterId, Long receiverId, String message);

    @Select("SELECT " +
            "fr.id, " +
            "fr.requester_id as requesterId, " +
            "fr.receiver_id as receiverId, " +
            "fr.request_message as requestMessage, " +
            "fr.status, " +
            "fr.create_time as createTime " +
            "FROM friend_requests fr " +
            "WHERE fr.receiver_id = #{userId} AND fr.status = 0")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "requesterId", column = "requesterId"),
        @Result(property = "receiverId", column = "receiverId"),
        @Result(property = "requestMessage", column = "requestMessage"),
        @Result(property = "status", column = "status"),
        @Result(property = "createTime", column = "createTime", javaType = Date.class)
    })
    List<FriendRequestVo> selectFriendRequests(@Param("userId") Long userId);

    @Select("SELECT " +
            "fr.id, " +
            "fr.requester_id as requesterId, " +
            "fr.receiver_id as receiverId, " +
            "fr.request_message as requestMessage, " +
            "fr.status, " +
            "fr.create_time as createTime " +
            "FROM friend_requests fr " +
            "WHERE fr.id = #{requestId} AND fr.status = 0")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "requesterId", column = "requesterId"),
        @Result(property = "receiverId", column = "receiverId"),
        @Result(property = "requestMessage", column = "requestMessage"),
        @Result(property = "status", column = "status"),
        @Result(property = "createTime", column = "createTime", javaType = Date.class)
    })
    FriendRequestVo selectFriendRequestById(@Param("requestId") Long requestId);

    @Update("UPDATE friend_requests SET status = #{status}, update_time = CURRENT_TIMESTAMP " +
            "WHERE id = #{requestId} AND status = 0")
    void updateFriendRequestStatus(@Param("requestId") Long requestId, @Param("status") Integer status);

    @Insert("INSERT INTO friendships(user_id, friend_id, status) " +
            "VALUES(#{userId}, #{friendId}, 1)")
    void insertFriendship(@Param("userId") Long userId, @Param("friendId") Long friendId);
}
